import { defineStore } from 'pinia'
import { ref } from 'vue'
import {GetSetting} from '@/api/common'
export const useCommonStore = defineStore('common', () => {
  const loading = ref<boolean>(false) // 是否加载中
  const userName = ref<string>('') // 用户名
  const setting = ref<SiteSetting>({
    fileDomain:'',
    tencentMapKey:'',
    giveAsk:0
  })
  if(localStorage.userName)  userName.value = localStorage.userName

  const quit = ():void => {
    localStorage.userName = ''
    userName.value = ''
    localStorage.refreshToken = ''
    localStorage.token  = ''
  }
  const getSetting = ():Promise<SiteSetting> => {
    return GetSetting().then((res:ServiceResponse<SiteSetting>) => {
       if(res.success && res.data) {
        setting.value = res.data
        setting.value.fileDomain = `https://${setting.value.fileDomain}/` 
       }
      return setting.value
    })
  }
  return { loading, userName, setting, getSetting, quit }
})
